﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;

namespace CSI300Cons.CapitalDownloader
{
    public static class MailHelper
    {
        private static string Mailto = "lancezhang@live.com";
        private static string MailFrom = "admin1986@gmail.com";
        private static string SMTPHOST = "smtp.gmail.com";
        private static string MailUID = "admin1986@gmail.com";
        private static string MailPWD = "35224550";
        private static bool EnableSsl = true;
        private static int MailPort = 456;

        /// <summary>
        /// 发送邮件
        /// </summary>
        public static void SendMailUseSMTP(string title,string message)
        {
            try
            {
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                msg.To.Add(Mailto);

                msg.From = new MailAddress(MailFrom, "Lance Zhang", System.Text.Encoding.UTF8);

                msg.Subject = title;//邮件标题 
                msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
                msg.Body = message;//邮件内容 
                msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
                msg.IsBodyHtml = true;//是否是HTML邮件 
                msg.Priority = MailPriority.High;//邮件优先级 

                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(MailUID, MailPWD);

                if (EnableSsl)
                {
                    client.Port = 587;//587;//Gmail使用的端口 
                    client.EnableSsl = true;//经过ssl加密 
                }

                client.Host = SMTPHOST;

                object userState = msg;

                client.Send(msg);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                string e = ex.Message;
            }
        }
    }
}
